﻿/*
Copyright 2009 Matías Fidemraizer (www.matiasfidemraizer.com).

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

using System;
using System.Linq;

#region "Custom namespace declarations"
using Comkarl;
#endregion

namespace Comkarl.Architecture.Patterns.MVC.Entities
{
    /// <summary>
    /// Represents the base class for an entity manager, that is a set of methods which may expose a list of entities and
    /// additional behaviors.
    /// </summary>
    /// <typeparam name="TIdentifierName">
    /// The type for the entities identifier's name retrieved by the underlying list.
    /// </typeparam>
    /// <typeparam name="TEntity">
    /// The type for the entities retrieved by the underlying list.
    /// </typeparam>
    public abstract class EntityManager<TIdentifierName, TEntity> : EntityList<TIdentifierName, TEntity>
        where TIdentifierName : IEquatable<TIdentifierName> 
        where TEntity : Entity<TIdentifierName>
    {
        #region "Indexers"
        /// <summary>
        /// Gets an entity from the underlying list.
        /// </summary>
        /// <param name="id">
        /// The identifier for an existing entity.
        /// </param>
        /// <returns>
        /// The entity for the provided identifier.
        /// </returns>
        new public TEntity this[Identifier<TIdentifierName> id]
        {
            get
            {
                return this.Where<TEntity>(someEntity => someEntity.ID == id).FirstOrDefault<TEntity>();
            }
        }
        #endregion
    }
}
